package com.dbb.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mortennobel.imagescaling.ResampleOp;

public class ImageManager {
	private static Logger LOGGER = LoggerFactory.getLogger(ImageManager.class);

	/**
	 * read image from file, now it can support image type:
	 * bmp,wbmp,gif,jpge,png
	 * 
	 * @param file
	 * @return BufferedImage
	 * 
	 * 
	 *         <pre>
	 * BufferedImage image;
	 * image = ImageUtils.readImage(new File(&quot;myImage.jpg&quot;));
	 * image = ImageUtils.readImage(new File(&quot;myImage.gif&quot;));
	 * image = ImageUtils.readImage(new File(&quot;myImage.bmp&quot;));
	 * image = ImageUtils.readImage(new File(&quot;myImage.png&quot;));
	 * </pre>
	 */
	BufferedImage image = null;
	
	public ImageManager() {}
	
	public ImageManager(String fileName) {
		readImage(new File(fileName));
	}
	
	public ImageManager(File file) {
		readImage(file);
	}
	
	public void resize(String dist, int high, int width) throws Exception {
		double[] size = zoomSize(image, high, width);
		writeImage(zoom(image, (int) size[0], (int) size[1]), "jpg", new File(dist));
	}
	
	public void resize(String filename, ImageSizeType type) throws Exception {
		double[] size = zoomSize(image, 100, 60);
		String filePath = Content.FILE_PATH + "Logo/" + filename + ".jpg";
		if(type == ImageSizeType.S) {
			size = zoomSize(image, 400, 300);
			filePath = Content.FILE_PATH + "S/" + filename + ".jpg";
		} else if (type == ImageSizeType.M) {
			size = zoomSize(image, 800, 600);
			filePath = Content.FILE_PATH + "M/" + filename + ".jpg";
		} else if  (type == ImageSizeType.L) {
			size = zoomSize(image, 1024, 768);
			filePath = Content.FILE_PATH + "L/" + filename + ".jpg";
		}
		writeImage(zoom(image, (int) size[0], (int) size[1]), "jpg", new File(filePath));
	}
	
	public static String getFilePath(String fileName, ImageSizeType type) {
		if(type == ImageSizeType.S) {
			return Content.FILE_PATH + "S/" + fileName;
		} else if (type == ImageSizeType.M) {
			return Content.FILE_PATH + "M/" + fileName;
		} else if  (type == ImageSizeType.L) {
			return Content.FILE_PATH + "L/" + fileName;
		} else {
			return Content.FILE_PATH + "Logo/" + fileName;
		}
	}
	
	public BufferedImage readImage(File file) {
		if (file != null && file.isFile() && file.exists()) {
			try {
				image = ImageIO.read(file);
			} catch (IOException e) {
				LOGGER.error("readImage(File)", "IOException", e);
			}
		}
		return image;
	}

	/**
	 * get the image width
	 * 
	 * @param image
	 * @return image width
	 */
	public double getWidth(BufferedImage image) {
		return image.getWidth();
	}

	/**
	 * get the image height
	 * 
	 * @param image
	 * @return image height
	 */
	public double getHeight(BufferedImage image) {
		return image.getHeight();
	}

	/**
	 * @param image
	 *            BufferedImage.
	 * @param width
	 *            zoom width.
	 * @param heigth
	 *            zoom heigth.
	 * @return BufferedImage
	 */
	public BufferedImage zoom(BufferedImage image, int width, int heigth) {
		ResampleOp resampleOp = new ResampleOp(width, heigth);
		BufferedImage tag = resampleOp.filter(image, null);
		return tag;
	}

	/**
	 * 
	 * @param image
	 *            BufferedImage
	 * @param width
	 *            zoom width.
	 * @param heigth
	 *            zoom heigth.
	 * @return double array.double[0]:width,double[1]:heigth.
	 */
	public double[] zoomSize(BufferedImage image, int width, int heigth) {
		double[] zoomSize = new double[2];
		double srcWidth = getWidth(image);
		double srcHeigth = getHeight(image);

		if (srcWidth > srcHeigth) {
			zoomSize[0] = (srcWidth / srcHeigth) * heigth;
			zoomSize[1] = heigth;
		}
		if (srcWidth < srcHeigth) {
			zoomSize[0] = width;
			zoomSize[1] = (srcHeigth / srcWidth) * width;
		}
		return zoomSize;
	}

	/**
	 * 
	 * Output to file out according to the style<code>BufferedImage</code> If
	 * can not appoint image or formateName or file out ,do nothing. Now it can
	 * support image type：bmp,wbmp,jpeg,png.
	 * 
	 * @param image
	 *            BufferedImage.
	 * @param formatName
	 *            format name.
	 * @param out
	 *            output path.
	 * @throws IOException
	 *             IOException
	 */
	public void writeImage(BufferedImage image, String formatName, File file)
			throws IOException {
		if (image != null && formatName != null && !"".equals(formatName)
				&& file != null) {
			ImageIO.write(image, formatName, file);
		}
	}


}
